package cn.edu.zafu.bigdata.linfeifei.I;

import java.io.*;
import java.net.*;

public class UdpTimeClient {
    public static void main(String[] args) {
        String serverAddress = "127.0.0.1"; // 服务端IP地址
        int port = 1234; // 服务端端口号

        try (DatagramSocket socket = new DatagramSocket()) {
            System.out.println("Sending request to UDP Time Server");

            String sentence = "What time is it?";
            byte[] sendData = sentence.getBytes();
            byte[] receiveData = new byte[1024];

            InetAddress address = InetAddress.getByName(serverAddress);
            DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, address, port);
            socket.send(sendPacket);

            DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
            socket.receive(receivePacket);

            String response = new String(receivePacket.getData(), 0, receivePacket.getLength());
            System.out.println("From server: " + response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
